//
//  MOAddressBookViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/8.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOAddressBookViewController.h"
#import "MOAddressBookViewCell.h"
#import "MOAddressBookSearchViewController.h"
#import "MOAddressBookPeopleViewController.h"

@interface MOAddressBookViewController ()<UITableViewDelegate,UITableViewDataSource>

//搜索
@property(nonatomic,strong)UIView *headerView;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;


@end

@implementation MOAddressBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
}

-(void)creatView
{
    self.title = @"通讯录";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.tableHeaderView = self.headerView;
    
}

-(void)initData
{
    self.dataSource = [NSMutableArray array];
//    NSArray *arr1 = @[@"伊犁洲税务局",@"塔城地区税务局",@"阿勒泰地区税务局"];
//    NSArray *arr2 = @[@"局领导",@"办公室",@"法制科",@"货物和劳务税科"];
//    for (int i = 0; i<arr1.count; i++) {
//        NSMutableArray *muArr = [NSMutableArray array];
//        for (int j = 0; j<arr2.count; j++) {
//            MOAddressBookModel *model = [[MOAddressBookModel alloc]init];
//            model.nameStr = arr2[j];
//            [muArr addObject:model];
//        }
//        [self.dataSource addObject:@{@"name":arr1[i],@"arr":muArr,@"isOpen":@"0"}];
//    }
//    [self.tableView reloadData];
    
    
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",ADDRESSBOOK_GETABBYORGTREE_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"通讯录架构:%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSArray *arr1 = responseObject;
        for (int i = 0; i<arr1.count; i++) {
            NSMutableArray *muArr = [NSMutableArray array];
            NSArray *arr2 = arr1[i][@"child"];
            for (int j = 0; j<arr2.count; j++) {
                MOAddressBookModel *model = [[MOAddressBookModel alloc]init];
                model.nameStr = arr2[j][@"org_name"];
                model.org_idStr = arr2[j][@"org_id"];
                model.org_nameStr = arr1[i][@"org_name"];
                [muArr addObject:model];
            }
            [self.dataSource addObject:@{@"name":arr1[i][@"org_name"],@"arr":muArr,@"isOpen":@"0",@"org_id":arr1[i][@"org_id"]}];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}



-(void)clickSearchTap
{
    MOAddressBookSearchViewController *vc = [[MOAddressBookSearchViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)clickOpenOrCloseButton:(UIButton *)sender
{
    sender.selected = !sender.selected;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dataSource[sender.tag]];
    if (sender.selected==1) {
        [dic setObject:@"1" forKey:@"isOpen"];
    }else{
        [dic setObject:@"0" forKey:@"isOpen"];
    }
    [self.dataSource replaceObjectAtIndex:sender.tag withObject:dic];
    [self.tableView reloadData];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.dataSource[section][@"arr"];
    NSDictionary *dic = self.dataSource[section];
    if ([dic[@"isOpen"] integerValue]==1) {
        return arr.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOAddressBookViewCell *cell = [MOAddressBookViewCell cellWithTableView:tableView andIndexPath:indexPath];
    MOAddressBookModel *model = self.dataSource[indexPath.section][@"arr"][indexPath.row];
    NSArray *arr = self.dataSource[indexPath.section][@"arr"];
    model.allCount = arr.count;
    model.cellIndex = indexPath.row;
    
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 64;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45)];
    view.backgroundColor = KWhiteColor;

    UIButton *iconButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 0, 43, 43)];
    iconButton.centerY = 64/2;
    iconButton.layer.masksToBounds = YES;
    iconButton.layer.cornerRadius = 43/2;
    if (section==0) {
        [iconButton setImage:IMAGE(@"address_zuzhi1") forState:UIControlStateNormal];
    }else{
        [iconButton setImage:IMAGE(@"address_zuzhi2") forState:UIControlStateNormal];
    }
    iconButton.userInteractionEnabled = NO;
    [view addSubview:iconButton];
    
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconButton.right +12, 0, SCREEN_WIDTH - 74*2, 20)];
    if (section==0) {
        nameLabel.font = [UIFont boldSystemFontOfSize:16];
    }else{
        nameLabel.font = FONT_SIZE(16);
    }
    nameLabel.centerY = 64/2;
    nameLabel.textColor = KBlackColor_2A2A2A;
    nameLabel.text = self.dataSource[section][@"name"];
    [view addSubview:nameLabel];
    
    NSDictionary *dic = self.dataSource[section];
    UIButton *openOrCloseButton = [[UIButton alloc]initWithFrame:CGRectMake(17, 0, SCREEN_WIDTH - 34, 64)];
    [openOrCloseButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    openOrCloseButton.tag = section;
    [openOrCloseButton addTarget:self action:@selector(clickOpenOrCloseButton:) forControlEvents:UIControlEventTouchUpInside];
    if ([dic[@"isOpen"] integerValue]==1) {
        openOrCloseButton.selected = YES;
    }else{
        openOrCloseButton.selected = NO;
    }
    openOrCloseButton.centerY = 64/2;
    [openOrCloseButton setImage:IMAGE(@"right_notice") forState:UIControlStateNormal];
    [openOrCloseButton setImage:IMAGE(@"top_notice") forState:UIControlStateSelected];
    [view addSubview:openOrCloseButton];
    
    UIView *oneLine = [[UIView alloc]initWithFrame:CGRectMake(72, 63, SCREEN_WIDTH-72, 1)];
    oneLine.backgroundColor = KGrayColor_ededed;
    [view addSubview:oneLine];
    if (section==self.dataSource.count-1) {
        oneLine.hidden = YES;
    }else{
        oneLine.hidden = NO;
    }
    
    UIView *twoLine = [[UIView alloc]initWithFrame:CGRectMake(37, 54, 1, 10)];
    twoLine.backgroundColor = KGrayColor_ededed;
    [view addSubview:twoLine];
    
    
    if ([dic[@"isOpen"] integerValue]==1) {
        twoLine.hidden = NO;
    }else{
        twoLine.hidden = YES;
    }
    
    return view;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOAddressBookModel *model = self.dataSource[indexPath.section][@"arr"][indexPath.row];
    MOAddressBookPeopleViewController *vc = [[MOAddressBookPeopleViewController alloc]init];
    vc.orginId = model.org_idStr;
    vc.orginName = self.dataSource[indexPath.section][@"name"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KGrayColor_F3F3F3;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(48)+20)];
        _headerView.backgroundColor = KWhiteColor;
        
        UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, kHConversions(32))];
        searchView.backgroundColor = KGrayColor_F3F3F3;
        searchView.layer.masksToBounds = YES;
        searchView.layer.cornerRadius = 3.0;
        [_headerView addSubview:searchView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickSearchTap)];
        [searchView addGestureRecognizer:tap];
        
        UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 100, kHConversions(32))];
        [searchButton setTitle:@" 搜索" forState:UIControlStateNormal];
        [searchButton setImage:IMAGE(@"address_search") forState:UIControlStateNormal];
        searchButton.titleLabel.font = FONT_SIZE(13);
        [searchButton setTitleColor:KGrayColor_ADADAD forState:UIControlStateNormal];
        searchButton.userInteractionEnabled = NO;
        [searchButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
        [searchView addSubview:searchButton];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, searchView.bottom + kHConversions(16), SCREEN_WIDTH, 10)];
        lineView.backgroundColor = KGrayColor_F3F3F3;
        [_headerView addSubview:lineView];

    }
    return _headerView;
}


@end
